home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / pluginy Firefox / 45975 / 45975.xpi / content / diclookup.js < prev    next >
Text File  |  2009-11-23  |  6KB  |  153 lines

  1. /*
  2.  * Copyright (c) 2009 Bui Viet Thanh (thanhbv@gmail.com).
  3.  *
  4.  * This file is part of clicknlearn.
  5.  *
  6.  * clicknlearn is free software: you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation, either version 3 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * clicknlearn is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with clicknlearn.  If not, see <http://www.gnu.org/licenses/>.
  18.  */
  19.  
  20. Components.utils.import("resource://clicknlearn/ext/Observers.js");
  21. Components.utils.import("resource://clicknlearn/cnlprefs.js");
  22.  
  23. function DicLookup(){
  24.     this.currentWord = "";
  25.     this.currentRemind = "";
  26.     this.remember = false;//has remember the current word
  27.     this.div = null;
  28.     this.divId = "clicknlearnDiv";
  29.     this.request = Object;
  30.  
  31.     this._initDicServices();
  32.     Observers.add("cnl_dicservicesPreferencesChanged", this._initDicServices, this);
  33.     
  34.     this._activeDicServiceIndex = this._defaultDicServiceIndex = 0;
  35. }
  36.  
  37. DicLookup.prototype._initDicServices = function() {    
  38.     var dicts = CNL_Prefs.getDicsArrayPref();
  39.     if(dicts.length == 0)
  40.         dicts =
  41.         [['true', 'tratu.vn', 'http://tratu.vn/dispatchaddon.php?dict=en_vn&title=',
  42.             '<i>Bß║ín ─æang tra tß╗½', '']
  43.         ,['true', 'vdict.com', 'http://vdict.com/fsearch.php?dictionaries=eng2vie_vie2eng_foldoc&word=',
  44.             ' <!-- Database --><!-- Database --><!-- Database -->Not Found.', '']
  45.         ,['true', 'edictx.com', 'http://edictx.com/edictx/?dict=1&mode=addon&word=',
  46.             ' was not found!<', '']
  47.         ,['true', 'tudientiengviet.net', 'http://www.tudientiengviet.net/get.php?mode=quickdict&dict=en-vi&word=',
  48.             'C├íc tß╗½ t╞░╞íng tß╗▒: <br><ul>', '']];
  49.     this.dicServices = [];
  50.     for(var i in dicts)
  51.         if(dicts[i][0] == "true")
  52.             this.dicServices.push(new DicService(dicts[i][1], dicts[i][2], dicts[i][3], dicts[i][4]));
  53. }
  54.  
  55. DicLookup.prototype.formatCurrentWord = function() {
  56.     this.currentWord = CNLUtils.formatWord(this.currentWord);    
  57. }
  58.  
  59. DicLookup.prototype.reset = function() {
  60.     this.clearRequest();
  61.     CNL.removeDiv(this.div);
  62.     this.div = null;
  63. }
  64.  
  65. DicLookup.prototype.clearRequest = function() {
  66.     if (this.request != null)
  67.         try {
  68.             this.request.abort();
  69.         } catch (e) {
  70.         }
  71.     this.request = null;
  72. }
  73.  
  74. DicLookup.prototype.printLookingUp = function() {
  75.     this.div.innerHTML = CNL.stringsBundle.getFormattedString('lookingup'
  76.             , [this.currentWord, this.dicServices[this._activeDicServiceIndex].name]);
  77. }
  78.  
  79. DicLookup.prototype.showDivWithEmptyWord = function() {
  80.     this.div.innerHTML = CNLUtils.replaceAntStyleString(
  81.             "<label for='cnlwordbox'>${lookup} </label>" +
  82.             "<input type='text' id='cnlwordbox' maxlength='60' size='15' value='' />");
  83.     var textbox = this.div.firstChild.nextSibling;
  84.     textbox.focus();
  85. }
  86.  
  87. DicLookup.prototype.requestWord = function() {
  88.     try    {
  89.         this.request = new XMLHttpRequest();
  90.         if (this.request.readyState != 0) {
  91.             return;
  92.         }
  93.     }
  94.     catch (e) {
  95.         return;
  96.     }
  97.     this._activeDicServiceIndex = this._defaultDicServiceIndex;
  98.     this.doRequestWord(false);
  99. }
  100.  
  101. DicLookup.prototype.doRequestWord = function(reRequestOnNotFound){
  102.     try {
  103.         this.request.open("GET", this.dicServices[this._activeDicServiceIndex].getUrl(this.currentWord), true);
  104.         this.request.send(null);
  105.     }
  106.     catch (e) {
  107.         return;
  108.     }
  109.  
  110.     this.request.onreadystatechange = function() {
  111.         // we have to use "CNL.DICLOOKUP.request.readyState" instead of "this.readyState"
  112.         if(! CNL.DICLOOKUP.div)
  113.             return;
  114.         if (CNL.DICLOOKUP.request.readyState == 4)
  115.             if (CNL.DICLOOKUP.request.status == 200){
  116.                 if (CNL.DICLOOKUP.dicServices[CNL.DICLOOKUP._activeDicServiceIndex].wordNotFound(CNL.DICLOOKUP.request.responseText)){
  117.                     if(reRequestOnNotFound)
  118.                         CNL.DICLOOKUP.requestWordFromOtherSources();
  119.                     else
  120.                         CNL.DICLOOKUP.div.innerHTML = CNL.stringsBundle.getFormattedString('wordnotfoundin',
  121.                                 [CNL.DICLOOKUP.currentWord, CNL.DICLOOKUP.dicServices[CNL.DICLOOKUP._activeDicServiceIndex].name])
  122.                             + " <button id='cnl_settingsButton'><img src='chrome://clicknlearn/skin/settings24.png' /></button>"
  123.                             +"<br /><button class='clicknlearnButton' id='requestWordFromOtherSourcesButton'>"
  124.                             + CNL.stringsBundle.getString('requestWordFromOtherSources') + '</button>';
  125.                 }
  126.                 else
  127.                     CNL.DICLOOKUP.dicServices[CNL.DICLOOKUP._activeDicServiceIndex].parseAndPrint(CNL.DICLOOKUP.request.responseText);
  128.             }
  129. //            else
  130. //                if (CNL.DICLOOKUP.request.status == 404)
  131. //                    CNL.DICLOOKUP.replaceLookingUp();
  132.     }
  133. }
  134.  
  135. DicLookup.prototype.requestWordFromOtherSources = function(){
  136.     this._activeDicServiceIndex ++;
  137.     if(this._activeDicServiceIndex >= this.dicServices.length)
  138.         this._activeDicServiceIndex = 0;
  139.     if (this._activeDicServiceIndex == this._defaultDicServiceIndex){
  140.         //Not found in all dic services.
  141.         this.div.innerHTML = CNL.stringsBundle.getFormattedString('wordnotfound', [this.currentWord]);
  142.         return;
  143.     }
  144.     this.printLookingUp();
  145.     this.doRequestWord(true);
  146. }
  147.  
  148. DicLookup.prototype.clearAll = function() {
  149.     CNL.removeDiv(this.div);
  150.     this.div = null;
  151.     this.currentWord = null;
  152. }
  153.